package com.tanguy.user.base;

import android.support.annotation.LayoutRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by DAB on 2017/4/12 22:55.
 */

public abstract class NullableAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "NullableAdapter";
    private static final int TYPE_NO_DATA = 0x9574;
    private static final int TYPE_FOOTER = 0x9575;
    private boolean hideFootView;
    public static boolean DEBUG = false;
    private OnRefreshListener mOnRefreshListener;


    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        mOnRefreshListener = onRefreshListener;
    }

    /**
     * 不再显示脚布局(比如加载更多的时候,没有更多的数据了,就可以隐藏掉)
     *
     * @param hideFootView
     */
    public void setHideFootView(boolean hideFootView) {
        this.hideFootView = hideFootView;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState != RecyclerView.SCROLL_STATE_IDLE)return;
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager instanceof LinearLayoutManager) {
                    int lastCompletelyVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
                    if (lastCompletelyVisibleItemPosition + 1 == getItemCount()) {
                        if (mOnRefreshListener != null && !hideFootView()) {
                            mOnRefreshListener.onRefresh();
                        }
                    }
                } else {
                    if (DEBUG)
                        Log.e(TAG, " 未找到:mLastCompletelyVisibleItemPosition,如果不是用LinearLayoutManager,需要传入mLastCompletelyVisibleItemPosition");
                    return;
                }


            }
        });
    }


    public abstract VH onCreateViewHolderLike(ViewGroup parent, int viewType);

    public abstract void onBindViewHolderLike(VH holder, int position);

    public abstract int getItemCountLike();

    public void onBindViewHolderLike(NoDataHolder holder, int position) {

    }

    public void onBindViewHolderLike(FooterHolder holder, int position) {
        holder.itemView.setVisibility(hideFootView() ? View.GONE : View.VISIBLE);
    }

    public int getItemViewTypeLike(int position) {
        return 0;
    }

    private boolean hideFootView() {
        return hideFootView;
    }

    @LayoutRes
    public int getNotDataLayout() {
        return 0;
    }

    @LayoutRes
    public int getFooterLayout() {
        return 0;
    }

    @Override
    public final int getItemCount() {
        int count = 0;
        int itemCountLike = getItemCountLike();
        do {
            if (itemCountLike == 0) {
                //判断是否需要显示无数据布局
                if (getNotDataLayout() != 0) {
                    count = 1;
                } else {
                    if (DEBUG) Log.e(TAG, " getNotDataLayout(): 未重写,跳过布局");
                }
                //直接不判断是否还有脚布局
                break;
            }
            count = itemCountLike;
            //有脚布局
            if (getFooterLayout() != 0) {
                count += 1;
            }

        } while (false);
        return count;
    }


    @SuppressWarnings("unchecked")
    @Override
    public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0 && holder.getClass().getName().equals(NoDataHolder.class.getName())) {
            onBindViewHolderLike((NoDataHolder) holder, position);
        } else if (position == getItemCountLike() && holder.getClass().getName().equals(FooterHolder.class.getName())) {
            onBindViewHolderLike((FooterHolder) holder, position);
        } else {
            onBindViewHolderLike((VH) holder, position);
        }

    }

    public NoDataHolder noDataHolder(View view) {
        return new NoDataHolder(view);
    }

    @Override
    public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_NO_DATA) {
            return noDataHolder(LayoutInflater.from(parent.getContext()).inflate(getNotDataLayout(), parent, false));
        }
        if (viewType == TYPE_FOOTER) {
            return new FooterHolder(LayoutInflater.from(parent.getContext()).inflate(getFooterLayout(), parent, false));
        }
        return onCreateViewHolderLike(parent, viewType);
    }

    @Override
    public final int getItemViewType(int position) {
        int viewType;
        int itemCountLike = getItemCountLike();
        do {
            if (position == 0 && getNotDataLayout() != 0 && itemCountLike == 0) {
                viewType = TYPE_NO_DATA;
                break;
            }
            if (position == itemCountLike && getFooterLayout() != 0 && itemCountLike > 0) {
                viewType = TYPE_FOOTER;
                break;
            }
            viewType = getItemViewTypeLike(position);

        } while (false);

        return viewType;
    }

    public class NoDataHolder extends RecyclerView.ViewHolder {
        public NoDataHolder(View inflate) {
            super(inflate);
        }
    }

    public class FooterHolder extends RecyclerView.ViewHolder {
        public FooterHolder(View inflate) {
            super(inflate);
        }
    }

    public interface OnRefreshListener {
        void onRefresh();
    }
}
